package com.genius.unite_platform.storedevice.service.impl;
import java.math.BigDecimal;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;

import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.genius.adminmanager.other.entity.FillAddJson;
import com.genius.adminmanager.other.entity.ResponseJson;
import com.genius.adminmanager.other.entity.SessionJson;
import com.genius.adminmanager.user.entity.UserJson;
import com.genius.unite_platform.storedevice.entity.StoreDeviceEntity;
import com.genius.unite_platform.storedevice.service.StoreDeviceServiceAdapter;
import com.genius.workflow.util.ConnectionException;
import com.genius.workflow.util.DBClosedException;
import com.genius.workflow.util.DBTools;
import com.genius.workflow.util.DBTools2;
import com.genius.workflow.util.DBUtils;

@Service("StoreDeviceEditService")
public class StoreDeviceEditServiceImpl extends StoreDeviceServiceAdapter{
	
	@Override
	public JSONObject edit(JSONObject json, JSONObject response,HttpSession session) {    	
    	JSONObject userJson = (JSONObject)session.getAttribute(SessionJson.currentUser);			
		json.put(StoreDeviceEntity.lrr, userJson.getString(UserJson.name));				
    	try {
    		JSONArray Arrayadd = new JSONArray();
    		JSONArray Arrayupdate = new JSONArray();   
    		JSONArray delData = new JSONArray();    		
    		int  totalnum=Integer.parseInt(json.getString("totalNum")); 
    	//	System.out.println(totalnum);
    		for(int i=0;i<totalnum;i++){
				JSONObject jsonObject = new JSONObject();				
				String flag=json.getString("flag_"+i);					
				Date day=new Date();
				SimpleDateFormat sdf= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");	
				jsonObject.put("storeno",json.getString("storeno"));
				jsonObject.put("deviceno",json.getString("deviceno_"+i));
				jsonObject.put("modelId",json.getString("modelId_"+i));				
				jsonObject.put("devicenum",json.getString("devicenum_"+i));
				//jsonObject.put("imgs",json.getString("img_"+i));	
				jsonObject.put("imgs","");	
				jsonObject.put("memo","");	
				jsonObject.put("lrr",userJson.getString(UserJson.name));	
				jsonObject.put("lrsj",sdf.format(day));					
								
				if (flag.equals("del"))
				{
					delData.add(jsonObject) ;
				}
				if (flag.equals("UID"))
				{
					Arrayupdate.add(jsonObject) ;
				}
				if (flag.equals("add"))
				{
					Arrayadd.add(jsonObject) ;	    	   
				}
			}
			    
		//	System.out.println(delData);
		//	System.out.println(Arrayupdate);
		//	System.out.println(Arrayadd);
		//	System.out.println(delData.size());
    		
			if(delData.size()>0)
			DBTools.deleteBach("storeno,deviceno,modelId", "storedevice",delData); 
		
    		//2.修改
			if(Arrayupdate.size()>0)
			{
    		String itemField = "devicenum,imgs,lrr,lrsj";
    		DBTools2.updateBatch(itemField, "storeno,deviceno,modelId", "storedevice", Arrayupdate);
			}
    		
    		//3.新增
			if (Arrayadd.size()>0)
			{
    		String insertfiled = "storeno,deviceno,modelId,devicenum,memo,imgs,lrr,lrsj";
			DBTools2.insertBatch(insertfiled,"storedevice",Arrayadd);	
			}
    		
			
			response.put(ResponseJson.status, ResponseJson.status_Success);
			response.put(ResponseJson.msg, ResponseJson.msg_Edit_Data_Success);
		}  catch (ConnectionException e) {
			response.put(ResponseJson.status, ResponseJson.status_Failed);
    		response.put(ResponseJson.msg, ResponseJson.msg_Edit_Data_Success);
		} catch (DBClosedException e) {
			response.put(ResponseJson.status, ResponseJson.status_Failed);
    		response.put(ResponseJson.msg, ResponseJson.msg_DB_Closed_Failed);
		} catch (SQLException e) {
			response.put(ResponseJson.status, ResponseJson.status_Failed);
			response.put(ResponseJson.msg, ResponseJson.msg_Edit_Data_Error);
		}
		return response;
	}
	
	@Override
	public JSONArray getStoreDevice(String storeno) {
		String sql = "select a.storeno,a.deviceno,b.devicename,a.modelId,c.modelname,c.modeltypeId,c.modeltype,a.devicenum,a.imgs from storedevice a,device b,devicemodel c "
				+ "where a.deviceno=b.deviceno "
				+ "and   b.deviceno=c.deviceno "
				+ "and   a.modelId=c.modelId  "
				+ "and   a.storeno= '"+storeno+"'";
		System.out.println(sql);
	    JSONArray jsonArray = new JSONArray();
        try {
            jsonArray = DBUtils.selectArr(sql,null);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return jsonArray;
	}
}

